/*
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Lepton.

The Initial Developer of the Original Code is Philippe Le Boulanger.
Portions created by Philippe Le Boulanger are Copyright (C) 2011 <lepton.phlb@gmail.com>.
All Rights Reserved.

Contributor(s): Jean-Jacques Pitrolle <lepton.jjp@gmail.com>.

Alternatively, the contents of this file may be used under the terms of the eCos GPL license
(the  [eCos GPL] License), in which case the provisions of [eCos GPL] License are applicable
instead of those above. If you wish to allow use of your version of this file only under the
terms of the [eCos GPL] License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting  the provisions above and replace
them with the notice and other provisions required by the [eCos GPL] License.
If you do not delete the provisions above, a recipient may use your version of this file under
either the MPL or the [eCos GPL] License."
*/


/*============================================
| Includes
==============================================*/
#include <string.h>
#include <stdio.h>

#include "kernel/core/kernelconf.h"

#if defined(USE_SEGGER)
   #include <ioat91sam9261.h>
   #include "at91sam9261_init.h"
#elif defined(USE_ECOS)
   #include "cyg/hal/at91sam9261.h"
#endif

#include "at91sam9261_lcd_lut.h"
#include "at91sam9261_lcd.h"

#include "kernel/dev/arch/arm9/at91sam9261/dev_at91sam9261_cpu/dev_at91sam9261_cpu.h"
/*============================================
| Global Declaration
==============================================*/
#define __inline inline


extern AT91PS_SYS AT91_SYS;

static char *lcd_console_address;               /* Start of console buffer         */

static int lcd_line_length;
static int lcd_color_fg;
static int lcd_color_bg;

static short console_col;
static short console_row;

static char lcd_is_enabled = 0;

static char* p_at91sam9261_frame_buffer[2]={
   (char*)AT91C_FRAME_BUFFER_0,
   (char*)AT91C_FRAME_BUFFER_1
};

static unsigned char at91sam9261_current_frame_buffer=0;
static unsigned char at91sam9261_shadow_frame_buffer=1;


static const PALETTEENTRY monochromePalette[256] =
{
   { 0x00, 0x00, 0x00, 0 },
   { 0xff, 0xff, 0xff, 0 }
};

const PALETTEENTRY rgbPalette[256] =
{
   { 0x00, 0x00, 0x00, 0 },     /* 0 Sys Black      gray 0  */
   { 0x80, 0x00, 0x00, 0 },     /* 1 Sys Dk Red  */
   { 0x00, 0x80, 0x00, 0 },     /* 2 Sys Dk Green  */
   { 0x80, 0x80, 0x00, 0 },     /* 3 Sys Dk Yellow  */
   { 0x00, 0x00, 0x80, 0 },     /* 4 Sys Dk Blue  */
   { 0x80, 0x00, 0x80, 0 },     /* 5 Sys Dk Violet  */
   { 0x00, 0x80, 0x80, 0 },     /* 6 Sys Dk Cyan  */
   { 0xc0, 0xc0, 0xc0, 0 },     /* 7 Sys Lt Grey    gray 192  */
   { 0xc0, 0xdc, 0xc0, 0 },     /* 8 Sys 8  */
   { 0xa6, 0xca, 0xf0, 0 },     /* 9 Sys 9 (the first 10 are fixed by Windows)  */
   { 0x04, 0x04, 0x04, 0 },     /* 10       gray 4  */
   { 0x08, 0x08, 0x08, 0 },     /* 11       gray 8  */
   { 0x0c, 0x0c, 0x0c, 0 },     /* 12       gray 12  */
   { 0x11, 0x11, 0x11, 0 },     /* 13       gray 17  */
   { 0x16, 0x16, 0x16, 0 },     /* 14       gray 22  */
   { 0x1c, 0x1c, 0x1c, 0 },     /* 15       gray 28  */
   { 0x22, 0x22, 0x22, 0 },     /* 16       gray 34  */
   { 0x29, 0x29, 0x29, 0 },     /* 17       gray 41  */
   { 0x55, 0x55, 0x55, 0 },     /* 18 swapped so inversions look good       gray 85  */
   { 0x4d, 0x4d, 0x4d, 0 },     /* 19 swapped so inversions look good       gray 77  */
   { 0x42, 0x42, 0x42, 0 },     /* 20 swapped so inversions look good       gray 66  */
   { 0x39, 0x39, 0x39, 0 },     /* 21 swapped so inversions look good       gray 57  */
   { 0xFF, 0x7C, 0x80, 0 },     /* 22 R255 G124 B128  */
   { 0xFF, 0x50, 0x50, 0 },     /* 23 R255 G80  B80  */
   { 0xD6, 0x00, 0x93, 0 },     /* 24 R214 G0   B147  */
   { 0xCC, 0xEC, 0xFF, 0 },     /* 25 R204 G236 B255  */
   { 0xEF, 0xD6, 0xC6, 0 },     /* 26 R239 G214 B198  */
   { 0xE7, 0xE7, 0xD6, 0 },     /* 27 R231 G231 B214  */
   { 0xAD, 0xA9, 0x90, 0 },     /* 28 R173 G169 B144  */
   { 0x33, 0x00, 0x00, 0 },     /* 29  */
   { 0x66, 0x00, 0x00, 0 },     /* 30  */
   { 0x99, 0x00, 0x00, 0 },     /* 31  */
   { 0xcc, 0x00, 0x00, 0 },     /* 32  */
   { 0x00, 0x33, 0x00, 0 },     /* 33  */
   { 0x33, 0x33, 0x00, 0 },     /* 34  */
   { 0x66, 0x33, 0x00, 0 },     /* 35  */
   { 0x99, 0x33, 0x00, 0 },     /* 36  */
   { 0xcc, 0x33, 0x00, 0 },     /* 37  */
   { 0xff, 0x33, 0x00, 0 },     /* 38  */
   { 0x00, 0x66, 0x00, 0 },     /* 39  */
   { 0x33, 0x66, 0x00, 0 },     /* 40  */
   { 0x66, 0x66, 0x00, 0 },     /* 41  */
   { 0x99, 0x66, 0x00, 0 },     /* 42  */
   { 0xcc, 0x66, 0x00, 0 },     /* 43  */
   { 0xff, 0x66, 0x00, 0 },     /* 44  */
   { 0x00, 0x99, 0x00, 0 },     /* 45  */
   { 0x33, 0x99, 0x00, 0 },     /* 46  */
   { 0x66, 0x99, 0x00, 0 },     /* 47  */
   { 0x99, 0x99, 0x00, 0 },     /* 48  */
   { 0xcc, 0x99, 0x00, 0 },     /* 49  */
   { 0xff, 0x99, 0x00, 0 },     /* 50  */
   { 0x00, 0xcc, 0x00, 0 },     /* 51  */
   { 0x33, 0xcc, 0x00, 0 },     /* 52  */
   { 0x66, 0xcc, 0x00, 0 },     /* 53  */
   { 0x99, 0xcc, 0x00, 0 },     /* 54  */
   { 0xcc, 0xcc, 0x00, 0 },     /* 55  */
   { 0xff, 0xcc, 0x00, 0 },     /* 56  */
   { 0x66, 0xff, 0x00, 0 },     /* 57  */
   { 0x99, 0xff, 0x00, 0 },     /* 58  */
   { 0xcc, 0xff, 0x00, 0 },     /* 59  */
   { 0x00, 0x00, 0x33, 0 },     /* 60  */
   { 0x33, 0x00, 0x33, 0 },     /* 61  */
   { 0x66, 0x00, 0x33, 0 },     /* 62  */
   { 0x99, 0x00, 0x33, 0 },     /* 63  */
   { 0xcc, 0x00, 0x33, 0 },     /* 64  */
   { 0xff, 0x00, 0x33, 0 },     /* 65  */
   { 0x00, 0x33, 0x33, 0 },     /* 66  */
   { 0x33, 0x33, 0x33, 0 },     /* 67       gray 51  */
   { 0x66, 0x33, 0x33, 0 },     /* 68  */
   { 0x99, 0x33, 0x33, 0 },     /* 69  */
   { 0xcc, 0x33, 0x33, 0 },     /* 70  */
   { 0xff, 0x33, 0x33, 0 },     /* 71  */
   { 0x00, 0x66, 0x33, 0 },     /* 72  */
   { 0x33, 0x66, 0x33, 0 },     /* 73  */
   { 0x66, 0x66, 0x33, 0 },     /* 74  */
   { 0x99, 0x66, 0x33, 0 },     /* 75  */
   { 0xcc, 0x66, 0x33, 0 },     /* 76  */
   { 0xff, 0x66, 0x33, 0 },     /* 77  */
   { 0x00, 0x99, 0x33, 0 },     /* 78  */
   { 0x33, 0x99, 0x33, 0 },     /* 79  */
   { 0x66, 0x99, 0x33, 0 },     /* 80  */
   { 0x99, 0x99, 0x33, 0 },     /* 81  */
   { 0xcc, 0x99, 0x33, 0 },     /* 82  */
   { 0xff, 0x99, 0x33, 0 },     /* 83  */
   { 0x00, 0xcc, 0x33, 0 },     /* 84  */
   { 0x33, 0xcc, 0x33, 0 },     /* 85  */
   { 0x66, 0xcc, 0x33, 0 },     /* 86  */
   { 0x99, 0xcc, 0x33, 0 },     /* 87  */
   { 0xcc, 0xcc, 0x33, 0 },     /* 88  */
   { 0xff, 0xcc, 0x33, 0 },     /* 89  */
   { 0x33, 0xff, 0x33, 0 },     /* 90  */
   { 0x66, 0xff, 0x33, 0 },     /* 91  */
   { 0x99, 0xff, 0x33, 0 },     /* 92  */
   { 0xcc, 0xff, 0x33, 0 },     /* 93  */
   { 0xff, 0xff, 0x33, 0 },     /* 94  */
   { 0x00, 0x00, 0x66, 0 },     /* 95  */
   { 0x33, 0x00, 0x66, 0 },     /* 96  */
   { 0x66, 0x00, 0x66, 0 },     /* 97  */
   { 0x99, 0x00, 0x66, 0 },     /* 98  */
   { 0xcc, 0x00, 0x66, 0 },     /* 99  */
   { 0xff, 0x00, 0x66, 0 },     /* 100  */
   { 0x00, 0x33, 0x66, 0 },     /* 101  */
   { 0x33, 0x33, 0x66, 0 },     /* 102  */
   { 0x66, 0x33, 0x66, 0 },     /* 103  */
   { 0x99, 0x33, 0x66, 0 },     /* 104  */
   { 0xcc, 0x33, 0x66, 0 },     /* 105  */
   { 0xff, 0x33, 0x66, 0 },     /* 106  */
   { 0x00, 0x66, 0x66, 0 },     /* 107  */
   { 0x33, 0x66, 0x66, 0 },     /* 108  */
   { 0x66, 0x66, 0x66, 0 },     /* 109      gray 102  */
   { 0x99, 0x66, 0x66, 0 },     /* 110  */
   { 0xcc, 0x66, 0x66, 0 },     /* 111  */
   { 0x00, 0x99, 0x66, 0 },     /* 112  */
   { 0x33, 0x99, 0x66, 0 },     /* 113  */
   { 0x66, 0x99, 0x66, 0 },     /* 114  */
   { 0x99, 0x99, 0x66, 0 },     /* 115  */
   { 0xcc, 0x99, 0x66, 0 },     /* 116  */
   { 0xff, 0x99, 0x66, 0 },     /* 117  */
   { 0x00, 0xcc, 0x66, 0 },     /* 118  */
   { 0x33, 0xcc, 0x66, 0 },     /* 119  */
   { 0x99, 0xcc, 0x66, 0 },     /* 120  */
   { 0xcc, 0xcc, 0x66, 0 },     /* 121  */
   { 0xff, 0xcc, 0x66, 0 },     /* 122  */
   { 0x00, 0xff, 0x66, 0 },     /* 123  */
   { 0x33, 0xff, 0x66, 0 },     /* 124  */
   { 0x99, 0xff, 0x66, 0 },     /* 125  */
   { 0xcc, 0xff, 0x66, 0 },     /* 126  */
   { 0xff, 0x00, 0xcc, 0 },     /* 127  */
   { 0xcc, 0x00, 0xff, 0 },     /* 128  */
   { 0x00, 0x99, 0x99, 0 },     /* 129  */
   { 0x99, 0x33, 0x99, 0 },     /* 130  */
   { 0x99, 0x00, 0x99, 0 },     /* 131  */
   { 0xcc, 0x00, 0x99, 0 },     /* 132  */
   { 0x00, 0x00, 0x99, 0 },     /* 133  */
   { 0x33, 0x33, 0x99, 0 },     /* 134  */
   { 0x66, 0x00, 0x99, 0 },     /* 135  */
   { 0xcc, 0x33, 0x99, 0 },     /* 136  */
   { 0xff, 0x00, 0x99, 0 },     /* 137  */
   { 0x00, 0x66, 0x99, 0 },     /* 138  */
   { 0x33, 0x66, 0x99, 0 },     /* 139  */
   { 0x66, 0x33, 0x99, 0 },     /* 140  */
   { 0x99, 0x66, 0x99, 0 },     /* 141  */
   { 0xcc, 0x66, 0x99, 0 },     /* 142  */
   { 0xff, 0x33, 0x99, 0 },     /* 143  */
   { 0x33, 0x99, 0x99, 0 },     /* 144  */
   { 0x66, 0x99, 0x99, 0 },     /* 145  */
   { 0x99, 0x99, 0x99, 0 },     /* 146      gray 153  */
   { 0xcc, 0x99, 0x99, 0 },     /* 147  */
   { 0xff, 0x99, 0x99, 0 },     /* 148  */
   { 0x00, 0xcc, 0x99, 0 },     /* 149  */
   { 0x33, 0xcc, 0x99, 0 },     /* 150  */
   { 0x66, 0xcc, 0x66, 0 },     /* 151  */
   { 0x99, 0xcc, 0x99, 0 },     /* 152  */
   { 0xcc, 0xcc, 0x99, 0 },     /* 153  */
   { 0xff, 0xcc, 0x99, 0 },     /* 154  */
   { 0x00, 0xff, 0x99, 0 },     /* 155  */
   { 0x33, 0xff, 0x99, 0 },     /* 156  */
   { 0x66, 0xcc, 0x99, 0 },     /* 157  */
   { 0x99, 0xff, 0x99, 0 },     /* 158  */
   { 0xcc, 0xff, 0x99, 0 },     /* 159  */
   { 0xff, 0xff, 0x99, 0 },     /* 160  */
   { 0x00, 0x00, 0xcc, 0 },     /* 161  */
   { 0x33, 0x00, 0x99, 0 },     /* 162  */
   { 0x66, 0x00, 0xcc, 0 },     /* 163  */
   { 0x99, 0x00, 0xcc, 0 },     /* 164  */
   { 0xcc, 0x00, 0xcc, 0 },     /* 165  */
   { 0x00, 0x33, 0x99, 0 },     /* 166  */
   { 0x33, 0x33, 0xcc, 0 },     /* 167  */
   { 0x66, 0x33, 0xcc, 0 },     /* 168  */
   { 0x99, 0x33, 0xcc, 0 },     /* 169  */
   { 0xcc, 0x33, 0xcc, 0 },     /* 170  */
   { 0xff, 0x33, 0xcc, 0 },     /* 171  */
   { 0x00, 0x66, 0xcc, 0 },     /* 172  */
   { 0x33, 0x66, 0xcc, 0 },     /* 173  */
   { 0x66, 0x66, 0x99, 0 },     /* 174  */
   { 0x99, 0x66, 0xcc, 0 },     /* 175  */
   { 0xcc, 0x66, 0xcc, 0 },     /* 176  */
   { 0xff, 0x66, 0x99, 0 },     /* 177  */
   { 0x00, 0x99, 0xcc, 0 },     /* 178  */
   { 0x33, 0x99, 0xcc, 0 },     /* 179  */
   { 0x66, 0x99, 0xcc, 0 },     /* 180  */
   { 0x99, 0x99, 0xcc, 0 },     /* 181  */
   { 0xcc, 0x99, 0xcc, 0 },     /* 182  */
   { 0xff, 0x99, 0xcc, 0 },     /* 183  */
   { 0x00, 0xcc, 0xcc, 0 },     /* 184  */
   { 0x33, 0xcc, 0xcc, 0 },     /* 185  */
   { 0x66, 0xcc, 0xcc, 0 },     /* 186  */
   { 0x99, 0xcc, 0xcc, 0 },     /* 187  */
   { 0xcc, 0xcc, 0xcc, 0 },     /* 188      gray 204  */
   { 0xff, 0xcc, 0xcc, 0 },     /* 189  */
   { 0x00, 0xff, 0xcc, 0 },     /* 190  */
   { 0x33, 0xff, 0xcc, 0 },     /* 191  */
   { 0x66, 0xff, 0x99, 0 },     /* 192  */
   { 0x99, 0xff, 0xcc, 0 },     /* 193  */
   { 0xcc, 0xff, 0xcc, 0 },     /* 194  */
   { 0xff, 0xff, 0xcc, 0 },     /* 195  */
   { 0x33, 0x00, 0xcc, 0 },     /* 196  */
   { 0x66, 0x00, 0xff, 0 },     /* 197  */
   { 0x99, 0x00, 0xff, 0 },     /* 198  */
   { 0x00, 0x33, 0xcc, 0 },     /* 199  */
   { 0x33, 0x33, 0xff, 0 },     /* 200  */
   { 0x66, 0x33, 0xff, 0 },     /* 201  */
   { 0x99, 0x33, 0xff, 0 },     /* 202  */
   { 0xcc, 0x33, 0xff, 0 },     /* 203  */
   { 0xff, 0x33, 0xff, 0 },     /* 204  */
   { 0x00, 0x66, 0xff, 0 },     /* 205  */
   { 0x33, 0x66, 0xff, 0 },     /* 206  */
   { 0x66, 0x66, 0xcc, 0 },     /* 207  */
   { 0x99, 0x66, 0xff, 0 },     /* 208  */
   { 0xcc, 0x66, 0xff, 0 },     /* 209  */
   { 0xff, 0x66, 0xcc, 0 },     /* 210  */
   { 0x00, 0x99, 0xff, 0 },     /* 211  */
   { 0x33, 0x99, 0xff, 0 },     /* 212  */
   { 0x66, 0x99, 0xff, 0 },     /* 213  */
   { 0x99, 0x99, 0xff, 0 },     /* 214  */
   { 0xcc, 0x99, 0xff, 0 },     /* 215  */
   { 0xff, 0x99, 0xff, 0 },     /* 216  */
   { 0x00, 0xcc, 0xff, 0 },     /* 217  */
   { 0x33, 0xcc, 0xff, 0 },     /* 218  */
   { 0x66, 0xcc, 0xff, 0 },     /* 219  */
   { 0x99, 0xcc, 0xff, 0 },     /* 220  */
   { 0xcc, 0xcc, 0xff, 0 },     /* 221  */
   { 0xff, 0xcc, 0xff, 0 },     /* 222  */
   { 0x33, 0xff, 0xff, 0 },     /* 223  */
   { 0x66, 0xff, 0xcc, 0 },     /* 224  */
   { 0x99, 0xff, 0xff, 0 },     /* 225  */
   { 0xcc, 0xff, 0xff, 0 },     /* 226  */
   { 0xff, 0x66, 0x66, 0 },     /* 227  */
   { 0x66, 0xff, 0x66, 0 },     /* 228  */
   { 0xff, 0xff, 0x66, 0 },     /* 229  */
   { 0x66, 0x66, 0xff, 0 },     /* 230  */
   { 0xff, 0x66, 0xff, 0 },     /* 231  */
   { 0x66, 0xff, 0xff, 0 },     /* 232  */
   { 0xA5, 0x00, 0x21, 0 },     /* 233      R165 G0 B33  */
   { 0x5f, 0x5f, 0x5f, 0 },     /* 234      gray 95  */
   { 0x77, 0x77, 0x77, 0 },     /* 235      gray 119  */
   { 0x86, 0x86, 0x86, 0 },     /* 236      gray 134  */
   { 0x96, 0x96, 0x96, 0 },     /* 237      gray 150  */
   { 0xcb, 0xcb, 0xcb, 0 },     /* 238      gray 203  */
   { 0xb2, 0xb2, 0xb2, 0 },     /* 239      gray 178  */
   { 0xd7, 0xd7, 0xd7, 0 },     /* 240      gray 215  */
   { 0xdd, 0xdd, 0xdd, 0 },     /* 241      gray 221  */
   { 0xe3, 0xe3, 0xe3, 0 },     /* 242      gray 227  */
   { 0xea, 0xea, 0xea, 0 },     /* 243      gray 234  */
   { 0xf1, 0xf1, 0xf1, 0 },     /* 244      gray 241  */
   { 0xf8, 0xf8, 0xf8, 0 },     /* 245      gray 248  */
   { 0xff, 0xfb, 0xf0, 0 },     /* 246 Sys Reserved  */
   { 0xa0, 0xa0, 0xa4, 0 },     /* 247 Sys Reserved  */
   { 0x80, 0x80, 0x80, 0 },     /* 248 Sys Lt Gray  gray 128  */
   { 0xff, 0x00, 0x00, 0 },     /* 249 Sys Red  */
   { 0x00, 0xff, 0x00, 0 },     /* 250 Sys Green  */
   { 0xff, 0xff, 0x00, 0 },     /* 251 Sys Yellow  */
   { 0x00, 0x00, 0xff, 0 },     /* 252 Sys Blue  */
   { 0xff, 0x00, 0xff, 0 },     /* 253 Sys Violet  */
   { 0x00, 0xff, 0xff, 0 },     /* 254 Sys Cyan  */
   { 0xff, 0xff, 0xff, 0 }      /* 255 Sys White     gray 255  */
};

const PALETTEENTRY paletteDIAG[256] =
{ {0x00,0x00,0x00,0x00},
  {0x80,0x00,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0x00,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0xc0,0xc0,0xc0,0x00},
  {0xc0,0xdc,0xc0,0x00},
  {0xa6,0xca,0xf0,0x00},
  {0x40,0x20,0x00,0x00},
  {0x60,0x20,0x00,0x00},
  {0x80,0x20,0x00,0x00},
  {0xa0,0x20,0x00,0x00},
  {0xc0,0x20,0x00,0x00},
  {0xe0,0x20,0x00,0x00},
  {0x00,0x40,0x00,0x00},
  {0x20,0x40,0x00,0x00},
  {0x40,0x40,0x00,0x00},
  {0x60,0x40,0x00,0x00},
  {0x80,0x40,0x00,0x00},
  {0xa0,0x40,0x00,0x00},
  {0xc0,0x40,0x00,0x00},
  {0xe0,0x40,0x00,0x00},
  {0x00,0x60,0x00,0x00},
  {0x20,0x60,0x00,0x00},
  {0x40,0x60,0x00,0x00},
  {0x60,0x60,0x00,0x00},
  {0x80,0x60,0x00,0x00},
  {0xa0,0x60,0x00,0x00},
  {0xc0,0x60,0x00,0x00},
  {0xe0,0x60,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x20,0x80,0x00,0x00},
  {0x40,0x80,0x00,0x00},
  {0x60,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0xa0,0x80,0x00,0x00},
  {0xc0,0x80,0x00,0x00},
  {0xe0,0x80,0x00,0x00},
  {0x00,0xa0,0x00,0x00},
  {0x20,0xa0,0x00,0x00},
  {0x40,0xa0,0x00,0x00},
  {0x60,0xa0,0x00,0x00},
  {0x80,0xa0,0x00,0x00},
  {0xa0,0xa0,0x00,0x00},
  {0xc0,0xa0,0x00,0x00},
  {0xe0,0xa0,0x00,0x00},
  {0x00,0xc0,0x00,0x00},
  {0x20,0xc0,0x00,0x00},
  {0x40,0xc0,0x00,0x00},
  {0x60,0xc0,0x00,0x00},
  {0x80,0xc0,0x00,0x00},
  {0xa0,0xc0,0x00,0x00},
  {0xc0,0xc0,0x00,0x00},
  {0xe0,0xc0,0x00,0x00},
  {0x00,0xe0,0x00,0x00},
  {0x20,0xe0,0x00,0x00},
  {0x40,0xe0,0x00,0x00},
  {0x60,0xe0,0x00,0x00},
  {0x80,0xe0,0x00,0x00},
  {0xa0,0xe0,0x00,0x00},
  {0xc0,0xe0,0x00,0x00},
  {0xe0,0xe0,0x00,0x00},
  {0x00,0x00,0x40,0x00},
  {0x20,0x00,0x40,0x00},
  {0x40,0x00,0x40,0x00},
  {0x60,0x00,0x40,0x00},
  {0x80,0x00,0x40,0x00},
  {0xa0,0x00,0x40,0x00},
  {0xc0,0x00,0x40,0x00},
  {0xe0,0x00,0x40,0x00},
  {0x00,0x20,0x40,0x00},
  {0x20,0x20,0x40,0x00},
  {0x40,0x20,0x40,0x00},
  {0x60,0x20,0x40,0x00},
  {0x80,0x20,0x40,0x00},
  {0xa0,0x20,0x40,0x00},
  {0xc0,0x20,0x40,0x00},
  {0xe0,0x20,0x40,0x00},
  {0x00,0x40,0x40,0x00},
  {0x20,0x40,0x40,0x00},
  {0x40,0x40,0x40,0x00},
  {0x60,0x40,0x40,0x00},
  {0x80,0x40,0x40,0x00},
  {0xa0,0x40,0x40,0x00},
  {0xc0,0x40,0x40,0x00},
  {0xe0,0x40,0x40,0x00},
  {0x00,0x60,0x40,0x00},
  {0x20,0x60,0x40,0x00},
  {0x40,0x60,0x40,0x00},
  {0x60,0x60,0x40,0x00},
  {0x80,0x60,0x40,0x00},
  {0xa0,0x60,0x40,0x00},
  {0xc0,0x60,0x40,0x00},
  {0xe0,0x60,0x40,0x00},
  {0x00,0x80,0x40,0x00},
  {0x20,0x80,0x40,0x00},
  {0x40,0x80,0x40,0x00},
  {0x60,0x80,0x40,0x00},
  {0x80,0x80,0x40,0x00},
  {0xa0,0x80,0x40,0x00},
  {0xc0,0x80,0x40,0x00},
  {0xe0,0x80,0x40,0x00},
  {0x00,0xa0,0x40,0x00},
  {0x20,0xa0,0x40,0x00},
  {0x40,0xa0,0x40,0x00},
  {0x60,0xa0,0x40,0x00},
  {0x80,0xa0,0x40,0x00},
  {0xa0,0xa0,0x40,0x00},
  {0xc0,0xa0,0x40,0x00},
  {0xe0,0xa0,0x40,0x00},
  {0x00,0xc0,0x40,0x00},
  {0x20,0xc0,0x40,0x00},
  {0x40,0xc0,0x40,0x00},
  {0x60,0xc0,0x40,0x00},
  {0x80,0xc0,0x40,0x00},
  {0xa0,0xc0,0x40,0x00},
  {0xc0,0xc0,0x40,0x00},
  {0xe0,0xc0,0x40,0x00},
  {0x00,0xe0,0x40,0x00},
  {0x20,0xe0,0x40,0x00},
  {0x40,0xe0,0x40,0x00},
  {0x60,0xe0,0x40,0x00},
  {0x80,0xe0,0x40,0x00},
  {0xa0,0xe0,0x40,0x00},
  {0xc0,0xe0,0x40,0x00},
  {0xe0,0xe0,0x40,0x00},
  {0x00,0x00,0x80,0x00},
  {0x20,0x00,0x80,0x00},
  {0x40,0x00,0x80,0x00},
  {0x60,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0xa0,0x00,0x80,0x00},
  {0xc0,0x00,0x80,0x00},
  {0xe0,0x00,0x80,0x00},
  {0x00,0x20,0x80,0x00},
  {0x20,0x20,0x80,0x00},
  {0x40,0x20,0x80,0x00},
  {0x60,0x20,0x80,0x00},
  {0x80,0x20,0x80,0x00},
  {0xa0,0x20,0x80,0x00},
  {0xc0,0x20,0x80,0x00},
  {0xe0,0x20,0x80,0x00},
  {0x00,0x40,0x80,0x00},
  {0x20,0x40,0x80,0x00},
  {0x40,0x40,0x80,0x00},
  {0x60,0x40,0x80,0x00},
  {0x80,0x40,0x80,0x00},
  {0xa0,0x40,0x80,0x00},
  {0xc0,0x40,0x80,0x00},
  {0xe0,0x40,0x80,0x00},
  {0x00,0x60,0x80,0x00},
  {0x20,0x60,0x80,0x00},
  {0x40,0x60,0x80,0x00},
  {0x60,0x60,0x80,0x00},
  {0x80,0x60,0x80,0x00},
  {0xa0,0x60,0x80,0x00},
  {0xc0,0x60,0x80,0x00},
  {0xe0,0x60,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0x20,0x80,0x80,0x00},
  {0x40,0x80,0x80,0x00},
  {0x60,0x80,0x80,0x00},
  {0x80,0x80,0x80,0x00},
  {0xa0,0x80,0x80,0x00},
  {0xc0,0x80,0x80,0x00},
  {0xe0,0x80,0x80,0x00},
  {0x00,0xa0,0x80,0x00},
  {0x20,0xa0,0x80,0x00},
  {0x40,0xa0,0x80,0x00},
  {0x60,0xa0,0x80,0x00},
  {0x80,0xa0,0x80,0x00},
  {0xa0,0xa0,0x80,0x00},
  {0xc0,0xa0,0x80,0x00},
  {0xe0,0xa0,0x80,0x00},
  {0x00,0xc0,0x80,0x00},
  {0x20,0xc0,0x80,0x00},
  {0x40,0xc0,0x80,0x00},
  {0x60,0xc0,0x80,0x00},
  {0x80,0xc0,0x80,0x00},
  {0xa0,0xc0,0x80,0x00},
  {0xc0,0xc0,0x80,0x00},
  {0xe0,0xc0,0x80,0x00},
  {0x00,0xe0,0x80,0x00},
  {0x20,0xe0,0x80,0x00},
  {0x40,0xe0,0x80,0x00},
  {0x60,0xe0,0x80,0x00},
  {0x80,0xe0,0x80,0x00},
  {0xa0,0xe0,0x80,0x00},
  {0xc0,0xe0,0x80,0x00},
  {0xe0,0xe0,0x80,0x00},
  {0x00,0x00,0xc0,0x00},
  {0x20,0x00,0xc0,0x00},
  {0x40,0x00,0xc0,0x00},
  {0x60,0x00,0xc0,0x00},
  {0x80,0x00,0xc0,0x00},
  {0xa0,0x00,0xc0,0x00},
  {0xc0,0x00,0xc0,0x00},
  {0xe0,0x00,0xc0,0x00},
  {0x00,0x20,0xc0,0x00},
  {0x20,0x20,0xc0,0x00},
  {0x40,0x20,0xc0,0x00},
  {0x60,0x20,0xc0,0x00},
  {0x80,0x20,0xc0,0x00},
  {0xa0,0x20,0xc0,0x00},
  {0xc0,0x20,0xc0,0x00},
  {0xe0,0x20,0xc0,0x00},
  {0x00,0x40,0xc0,0x00},
  {0x20,0x40,0xc0,0x00},
  {0x40,0x40,0xc0,0x00},
  {0x60,0x40,0xc0,0x00},
  {0x80,0x40,0xc0,0x00},
  {0xa0,0x40,0xc0,0x00},
  {0xc0,0x40,0xc0,0x00},
  {0xe0,0x40,0xc0,0x00},
  {0x00,0x60,0xc0,0x00},
  {0x20,0x60,0xc0,0x00},
  {0x40,0x60,0xc0,0x00},
  {0x60,0x60,0xc0,0x00},
  {0x80,0x60,0xc0,0x00},
  {0xa0,0x60,0xc0,0x00},
  {0xc0,0x60,0xc0,0x00},
  {0xe0,0x60,0xc0,0x00},
  {0x00,0x80,0xc0,0x00},
  {0x20,0x80,0xc0,0x00},
  {0x40,0x80,0xc0,0x00},
  {0x60,0x80,0xc0,0x00},
  {0x80,0x80,0xc0,0x00},
  {0xa0,0x80,0xc0,0x00},
  {0xc0,0x80,0xc0,0x00},
  {0xe0,0x80,0xc0,0x00},
  {0x00,0xa0,0xc0,0x00},
  {0x20,0xa0,0xc0,0x00},
  {0x40,0xa0,0xc0,0x00},
  {0x60,0xa0,0xc0,0x00},
  {0x80,0xa0,0xc0,0x00},
  {0xa0,0xa0,0xc0,0x00},
  {0xc0,0xa0,0xc0,0x00},
  {0xe0,0xa0,0xc0,0x00},
  {0x00,0xc0,0xc0,0x00},
  {0x20,0xc0,0xc0,0x00},
  {0x40,0xc0,0xc0,0x00},
  {0x60,0xc0,0xc0,0x00},
  {0x80,0xc0,0xc0,0x00},
  {0xa0,0xc0,0xc0,0x00},
  {0xff,0xfb,0xf0,0x00},
  {0xa0,0xa0,0xa4,0x00},
  {0x80,0x80,0x80,0x00},
  {0xff,0x00,0x00,0x00},
  {0x00,0xff,0x00,0x00},
  {0xff,0xff,0x00,0x00},
  {0x00,0x00,0xff,0x00},
  {0xff,0x00,0xff,0x00},
  {0x00,0xff,0xff,0x00},
  {0xff,0xff,0xff,0x00}};

const PALETTEENTRY PalettePC2[256] =
{ {0x00,0x00,0x00,0x00},
  {0x80,0x00,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0x00,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0xc0,0xc0,0xc0,0x00},
  {0xc0,0xdc,0xc0,0x00},
  {0xa6,0xca,0xf0,0x00},
  {0x40,0x20,0x00,0x00},
  {0x60,0x20,0x00,0x00},
  {0x80,0x20,0x00,0x00},
  {0xa0,0x20,0x00,0x00},
  {0xc0,0x20,0x00,0x00},
  {0xe0,0x20,0x00,0x00},
  {0x00,0x40,0x00,0x00},
  {0x20,0x40,0x00,0x00},
  {0x40,0x40,0x00,0x00},
  {0x60,0x40,0x00,0x00},
  {0x80,0x40,0x00,0x00},
  {0xa0,0x40,0x00,0x00},
  {0xc0,0x40,0x00,0x00},
  {0xe0,0x40,0x00,0x00},
  {0x00,0x60,0x00,0x00},
  {0x20,0x60,0x00,0x00},
  {0x40,0x60,0x00,0x00},
  {0x60,0x60,0x00,0x00},
  {0x80,0x60,0x00,0x00},
  {0xa0,0x60,0x00,0x00},
  {0xc0,0x60,0x00,0x00},
  {0xe0,0x60,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x20,0x80,0x00,0x00},
  {0x40,0x80,0x00,0x00},
  {0x60,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0xa0,0x80,0x00,0x00},
  {0xc0,0x80,0x00,0x00},
  {0xe0,0x80,0x00,0x00},
  {0x00,0xa0,0x00,0x00},
  {0x20,0xa0,0x00,0x00},
  {0x40,0xa0,0x00,0x00},
  {0x60,0xa0,0x00,0x00},
  {0x80,0xa0,0x00,0x00},
  {0xa0,0xa0,0x00,0x00},
  {0xc0,0xa0,0x00,0x00},
  {0xe0,0xa0,0x00,0x00},
  {0x00,0xc0,0x00,0x00},
  {0x20,0xc0,0x00,0x00},
  {0x40,0xc0,0x00,0x00},
  {0x60,0xc0,0x00,0x00},
  {0x80,0xc0,0x00,0x00},
  {0xa0,0xc0,0x00,0x00},
  {0xc0,0xc0,0x00,0x00},
  {0xe0,0xc0,0x00,0x00},
  {0x00,0xe0,0x00,0x00},
  {0x20,0xe0,0x00,0x00},
  {0x40,0xe0,0x00,0x00},
  {0x60,0xe0,0x00,0x00},
  {0x80,0xe0,0x00,0x00},
  {0xa0,0xe0,0x00,0x00},
  {0xc0,0xe0,0x00,0x00},
  {0xe0,0xe0,0x00,0x00},
  {0x00,0x00,0x40,0x00},
  {0x20,0x00,0x40,0x00},
  {0x40,0x00,0x40,0x00},
  {0x60,0x00,0x40,0x00},
  {0x80,0x00,0x40,0x00},
  {0xa0,0x00,0x40,0x00},
  {0xc0,0x00,0x40,0x00},
  {0xe0,0x00,0x40,0x00},
  {0x00,0x20,0x40,0x00},
  {0x20,0x20,0x40,0x00},
  {0x40,0x20,0x40,0x00},
  {0x60,0x20,0x40,0x00},
  {0x80,0x20,0x40,0x00},
  {0xa0,0x20,0x40,0x00},
  {0xc0,0x20,0x40,0x00},
  {0xe0,0x20,0x40,0x00},
  {0x00,0x40,0x40,0x00},
  {0x20,0x40,0x40,0x00},
  {0x40,0x40,0x40,0x00},
  {0x60,0x40,0x40,0x00},
  {0x80,0x40,0x40,0x00},
  {0xa0,0x40,0x40,0x00},
  {0xc0,0x40,0x40,0x00},
  {0xe0,0x40,0x40,0x00},
  {0x00,0x60,0x40,0x00},
  {0x20,0x60,0x40,0x00},
  {0x40,0x60,0x40,0x00},
  {0x60,0x60,0x40,0x00},
  {0x80,0x60,0x40,0x00},
  {0xa0,0x60,0x40,0x00},
  {0xc0,0x60,0x40,0x00},
  {0xe0,0x60,0x40,0x00},
  {0x00,0x80,0x40,0x00},
  {0x20,0x80,0x40,0x00},
  {0x40,0x80,0x40,0x00},
  {0x60,0x80,0x40,0x00},
  {0x80,0x80,0x40,0x00},
  {0xa0,0x80,0x40,0x00},
  {0xc0,0x80,0x40,0x00},
  {0xe0,0x80,0x40,0x00},
  {0x00,0xa0,0x40,0x00},
  {0x20,0xa0,0x40,0x00},
  {0x40,0xa0,0x40,0x00},
  {0x60,0xa0,0x40,0x00},
  {0x80,0xa0,0x40,0x00},
  {0xa0,0xa0,0x40,0x00},
  {0xc0,0xa0,0x40,0x00},
  {0xe0,0xa0,0x40,0x00},
  {0x00,0xc0,0x40,0x00},
  {0x20,0xc0,0x40,0x00},
  {0x40,0xc0,0x40,0x00},
  {0x60,0xc0,0x40,0x00},
  {0x80,0xc0,0x40,0x00},
  {0xa0,0xc0,0x40,0x00},
  {0xc0,0xc0,0x40,0x00},
  {0xe0,0xc0,0x40,0x00},
  {0x00,0xe0,0x40,0x00},
  {0x20,0xe0,0x40,0x00},
  {0x40,0xe0,0x40,0x00},
  {0x60,0xe0,0x40,0x00},
  {0x80,0xe0,0x40,0x00},
  {0xa0,0xe0,0x40,0x00},
  {0xc0,0xe0,0x40,0x00},
  {0xe0,0xe0,0x40,0x00},
  {0x00,0x00,0x80,0x00},
  {0x20,0x00,0x80,0x00},
  {0x40,0x00,0x80,0x00},
  {0x60,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0xa0,0x00,0x80,0x00},
  {0xc0,0x00,0x80,0x00},
  {0xe0,0x00,0x80,0x00},
  {0x00,0x20,0x80,0x00},
  {0x20,0x20,0x80,0x00},
  {0x40,0x20,0x80,0x00},
  {0x60,0x20,0x80,0x00},
  {0x80,0x20,0x80,0x00},
  {0xa0,0x20,0x80,0x00},
  {0xc0,0x20,0x80,0x00},
  {0xe0,0x20,0x80,0x00},
  {0x00,0x40,0x80,0x00},
  {0x20,0x40,0x80,0x00},
  {0x40,0x40,0x80,0x00},
  {0x60,0x40,0x80,0x00},
  {0x80,0x40,0x80,0x00},
  {0xa0,0x40,0x80,0x00},
  {0xc0,0x40,0x80,0x00},
  {0xe0,0x40,0x80,0x00},
  {0x00,0x60,0x80,0x00},
  {0x20,0x60,0x80,0x00},
  {0x40,0x60,0x80,0x00},
  {0x60,0x60,0x80,0x00},
  {0x80,0x60,0x80,0x00},
  {0xa0,0x60,0x80,0x00},
  {0xc0,0x60,0x80,0x00},
  {0xe0,0x60,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0x20,0x80,0x80,0x00},
  {0x40,0x80,0x80,0x00},
  {0x60,0x80,0x80,0x00},
  {0x80,0x80,0x80,0x00},
  {0xa0,0x80,0x80,0x00},
  {0xc0,0x80,0x80,0x00},
  {0xe0,0x80,0x80,0x00},
  {0x00,0xa0,0x80,0x00},
  {0x20,0xa0,0x80,0x00},
  {0x40,0xa0,0x80,0x00},
  {0x60,0xa0,0x80,0x00},
  {0x80,0xa0,0x80,0x00},
  {0xa0,0xa0,0x80,0x00},
  {0xc0,0xa0,0x80,0x00},
  {0xe0,0xa0,0x80,0x00},
  {0x00,0xc0,0x80,0x00},
  {0x20,0xc0,0x80,0x00},
  {0x40,0xc0,0x80,0x00},
  {0x60,0xc0,0x80,0x00},
  {0x80,0xc0,0x80,0x00},
  {0xa0,0xc0,0x80,0x00},
  {0xc0,0xc0,0x80,0x00},
  {0xe0,0xc0,0x80,0x00},
  {0x00,0xe0,0x80,0x00},
  {0x20,0xe0,0x80,0x00},
  {0x40,0xe0,0x80,0x00},
  {0x60,0xe0,0x80,0x00},
  {0x80,0xe0,0x80,0x00},
  {0xa0,0xe0,0x80,0x00},
  {0xc0,0xe0,0x80,0x00},
  {0xe0,0xe0,0x80,0x00},
  {0x00,0x00,0xc0,0x00},
  {0x20,0x00,0xc0,0x00},
  {0x40,0x00,0xc0,0x00},
  {0x60,0x00,0xc0,0x00},
  {0x80,0x00,0xc0,0x00},
  {0xa0,0x00,0xc0,0x00},
  {0xc0,0x00,0xc0,0x00},
  {0xe0,0x00,0xc0,0x00},
  {0x00,0x20,0xc0,0x00},
  {0x20,0x20,0xc0,0x00},
  {0x40,0x20,0xc0,0x00},
  {0x60,0x20,0xc0,0x00},
  {0x80,0x20,0xc0,0x00},
  {0xa0,0x20,0xc0,0x00},
  {0xc0,0x20,0xc0,0x00},
  {0xe0,0x20,0xc0,0x00},
  {0x00,0x40,0xc0,0x00},
  {0x20,0x40,0xc0,0x00},
  {0x40,0x40,0xc0,0x00},
  {0x60,0x40,0xc0,0x00},
  {0x80,0x40,0xc0,0x00},
  {0xa0,0x40,0xc0,0x00},
  {0xc0,0x40,0xc0,0x00},
  {0xe0,0x40,0xc0,0x00},
  {0x00,0x60,0xc0,0x00},
  {0x20,0x60,0xc0,0x00},
  {0x40,0x60,0xc0,0x00},
  {0x60,0x60,0xc0,0x00},
  {0x80,0x60,0xc0,0x00},
  {0xa0,0x60,0xc0,0x00},
  {0xc0,0x60,0xc0,0x00},
  {0xe0,0x60,0xc0,0x00},
  {0x00,0x80,0xc0,0x00},
  {0x20,0x80,0xc0,0x00},
  {0x40,0x80,0xc0,0x00},
  {0x60,0x80,0xc0,0x00},
  {0x80,0x80,0xc0,0x00},
  {0xa0,0x80,0xc0,0x00},
  {0xc0,0x80,0xc0,0x00},
  {0xe0,0x80,0xc0,0x00},
  {0x00,0xa0,0xc0,0x00},
  {0x20,0xa0,0xc0,0x00},
  {0x40,0xa0,0xc0,0x00},
  {0x60,0xa0,0xc0,0x00},
  {0x80,0xa0,0xc0,0x00},
  {0xa0,0xa0,0xc0,0x00},
  {0xc0,0xa0,0xc0,0x00},
  {0xe0,0xa0,0xc0,0x00},
  {0x00,0xc0,0xc0,0x00},
  {0x20,0xc0,0xc0,0x00},
  {0x40,0xc0,0xc0,0x00},
  {0x60,0xc0,0xc0,0x00},
  {0x80,0xc0,0xc0,0x00},
  {0xa0,0xc0,0xc0,0x00},
  {0xff,0xfb,0xf0,0x00},
  {0xa0,0xa0,0xa4,0x00},
  {0x80,0x80,0x80,0x00},
  {0xff,0x00,0x00,0x00},
  {0x00,0xff,0x00,0x00},
  {0xff,0xff,0x00,0x00},
  {0x00,0x00,0xff,0x00},
  {0xff,0x00,0xff,0x00},
  {0x00,0xff,0xff,0x00},
  {0xff,0xff,0xff,0x00}};

static const PALETTEENTRY palette[256] =
{ {0x00,0x00,0x00,0x00},
  {0x80,0x00,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0x00,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0xc0,0xc0,0xc0,0x00},
  {0xc0,0xdc,0xc0,0x00},
  {0xa6,0xca,0xf0,0x00},
  {0x40,0x20,0x00,0x00},
  {0x60,0x20,0x00,0x00},
  {0x80,0x20,0x00,0x00},
  {0xa0,0x20,0x00,0x00},
  {0xc0,0x20,0x00,0x00},
  {0xe0,0x20,0x00,0x00},
  {0x00,0x40,0x00,0x00},
  {0x20,0x40,0x00,0x00},
  {0x40,0x40,0x00,0x00},
  {0x60,0x40,0x00,0x00},
  {0x80,0x40,0x00,0x00},
  {0xa0,0x40,0x00,0x00},
  {0xc0,0x40,0x00,0x00},
  {0xe0,0x40,0x00,0x00},
  {0x00,0x60,0x00,0x00},
  {0x20,0x60,0x00,0x00},
  {0x40,0x60,0x00,0x00},
  {0x60,0x60,0x00,0x00},
  {0x80,0x60,0x00,0x00},
  {0xa0,0x60,0x00,0x00},
  {0xc0,0x60,0x00,0x00},
  {0xe0,0x60,0x00,0x00},
  {0x00,0x80,0x00,0x00},
  {0x20,0x80,0x00,0x00},
  {0x40,0x80,0x00,0x00},
  {0x60,0x80,0x00,0x00},
  {0x80,0x80,0x00,0x00},
  {0xa0,0x80,0x00,0x00},
  {0xc0,0x80,0x00,0x00},
  {0xe0,0x80,0x00,0x00},
  {0x00,0xa0,0x00,0x00},
  {0x20,0xa0,0x00,0x00},
  {0x40,0xa0,0x00,0x00},
  {0x60,0xa0,0x00,0x00},
  {0x80,0xa0,0x00,0x00},
  {0xa0,0xa0,0x00,0x00},
  {0xc0,0xa0,0x00,0x00},
  {0xe0,0xa0,0x00,0x00},
  {0x00,0xc0,0x00,0x00},
  {0x20,0xc0,0x00,0x00},
  {0x40,0xc0,0x00,0x00},
  {0x60,0xc0,0x00,0x00},
  {0x80,0xc0,0x00,0x00},
  {0xa0,0xc0,0x00,0x00},
  {0xc0,0xc0,0x00,0x00},
  {0xe0,0xc0,0x00,0x00},
  {0x00,0xe0,0x00,0x00},
  {0x20,0xe0,0x00,0x00},
  {0x40,0xe0,0x00,0x00},
  {0x60,0xe0,0x00,0x00},
  {0x80,0xe0,0x00,0x00},
  {0xa0,0xe0,0x00,0x00},
  {0xc0,0xe0,0x00,0x00},
  {0xe0,0xe0,0x00,0x00},
  {0x00,0x00,0x40,0x00},
  {0x20,0x00,0x40,0x00},
  {0x40,0x00,0x40,0x00},
  {0x60,0x00,0x40,0x00},
  {0x80,0x00,0x40,0x00},
  {0xa0,0x00,0x40,0x00},
  {0xc0,0x00,0x40,0x00},
  {0xe0,0x00,0x40,0x00},
  {0x00,0x20,0x40,0x00},
  {0x20,0x20,0x40,0x00},
  {0x40,0x20,0x40,0x00},
  {0x60,0x20,0x40,0x00},
  {0x80,0x20,0x40,0x00},
  {0xa0,0x20,0x40,0x00},
  {0xc0,0x20,0x40,0x00},
  {0xe0,0x20,0x40,0x00},
  {0x00,0x40,0x40,0x00},
  {0x20,0x40,0x40,0x00},
  {0x40,0x40,0x40,0x00},
  {0x60,0x40,0x40,0x00},
  {0x80,0x40,0x40,0x00},
  {0xa0,0x40,0x40,0x00},
  {0xc0,0x40,0x40,0x00},
  {0xe0,0x40,0x40,0x00},
  {0x00,0x60,0x40,0x00},
  {0x20,0x60,0x40,0x00},
  {0x40,0x60,0x40,0x00},
  {0x60,0x60,0x40,0x00},
  {0x80,0x60,0x40,0x00},
  {0xa0,0x60,0x40,0x00},
  {0xc0,0x60,0x40,0x00},
  {0xe0,0x60,0x40,0x00},
  {0x00,0x80,0x40,0x00},
  {0x20,0x80,0x40,0x00},
  {0x40,0x80,0x40,0x00},
  {0x60,0x80,0x40,0x00},
  {0x80,0x80,0x40,0x00},
  {0xa0,0x80,0x40,0x00},
  {0xc0,0x80,0x40,0x00},
  {0xe0,0x80,0x40,0x00},
  {0x00,0xa0,0x40,0x00},
  {0x20,0xa0,0x40,0x00},
  {0x40,0xa0,0x40,0x00},
  {0x60,0xa0,0x40,0x00},
  {0x80,0xa0,0x40,0x00},
  {0xa0,0xa0,0x40,0x00},
  {0xc0,0xa0,0x40,0x00},
  {0xe0,0xa0,0x40,0x00},
  {0x00,0xc0,0x40,0x00},
  {0x20,0xc0,0x40,0x00},
  {0x40,0xc0,0x40,0x00},
  {0x60,0xc0,0x40,0x00},
  {0x80,0xc0,0x40,0x00},
  {0xa0,0xc0,0x40,0x00},
  {0xc0,0xc0,0x40,0x00},
  {0xe0,0xc0,0x40,0x00},
  {0x00,0xe0,0x40,0x00},
  {0x20,0xe0,0x40,0x00},
  {0x40,0xe0,0x40,0x00},
  {0x60,0xe0,0x40,0x00},
  {0x80,0xe0,0x40,0x00},
  {0xa0,0xe0,0x40,0x00},
  {0xc0,0xe0,0x40,0x00},
  {0xe0,0xe0,0x40,0x00},
  {0x00,0x00,0x80,0x00},
  {0x20,0x00,0x80,0x00},
  {0x40,0x00,0x80,0x00},
  {0x60,0x00,0x80,0x00},
  {0x80,0x00,0x80,0x00},
  {0xa0,0x00,0x80,0x00},
  {0xc0,0x00,0x80,0x00},
  {0xe0,0x00,0x80,0x00},
  {0x00,0x20,0x80,0x00},
  {0x20,0x20,0x80,0x00},
  {0x40,0x20,0x80,0x00},
  {0x60,0x20,0x80,0x00},
  {0x80,0x20,0x80,0x00},
  {0xa0,0x20,0x80,0x00},
  {0xc0,0x20,0x80,0x00},
  {0xe0,0x20,0x80,0x00},
  {0x00,0x40,0x80,0x00},
  {0x20,0x40,0x80,0x00},
  {0x40,0x40,0x80,0x00},
  {0x60,0x40,0x80,0x00},
  {0x80,0x40,0x80,0x00},
  {0xa0,0x40,0x80,0x00},
  {0xc0,0x40,0x80,0x00},
  {0xe0,0x40,0x80,0x00},
  {0x00,0x60,0x80,0x00},
  {0x20,0x60,0x80,0x00},
  {0x40,0x60,0x80,0x00},
  {0x60,0x60,0x80,0x00},
  {0x80,0x60,0x80,0x00},
  {0xa0,0x60,0x80,0x00},
  {0xc0,0x60,0x80,0x00},
  {0xe0,0x60,0x80,0x00},
  {0x00,0x80,0x80,0x00},
  {0x20,0x80,0x80,0x00},
  {0x40,0x80,0x80,0x00},
  {0x60,0x80,0x80,0x00},
  {0x80,0x80,0x80,0x00},
  {0xa0,0x80,0x80,0x00},
  {0xc0,0x80,0x80,0x00},
  {0xe0,0x80,0x80,0x00},
  {0x00,0xa0,0x80,0x00},
  {0x20,0xa0,0x80,0x00},
  {0x40,0xa0,0x80,0x00},
  {0x60,0xa0,0x80,0x00},
  {0x80,0xa0,0x80,0x00},
  {0xa0,0xa0,0x80,0x00},
  {0xc0,0xa0,0x80,0x00},
  {0xe0,0xa0,0x80,0x00},
  {0x00,0xc0,0x80,0x00},
  {0x20,0xc0,0x80,0x00},
  {0x40,0xc0,0x80,0x00},
  {0x60,0xc0,0x80,0x00},
  {0x80,0xc0,0x80,0x00},
  {0xa0,0xc0,0x80,0x00},
  {0xc0,0xc0,0x80,0x00},
  {0xe0,0xc0,0x80,0x00},
  {0x00,0xe0,0x80,0x00},
  {0x20,0xe0,0x80,0x00},
  {0x40,0xe0,0x80,0x00},
  {0x60,0xe0,0x80,0x00},
  {0x80,0xe0,0x80,0x00},
  {0xa0,0xe0,0x80,0x00},
  {0xc0,0xe0,0x80,0x00},
  {0xe0,0xe0,0x80,0x00},
  {0x00,0x00,0xc0,0x00},
  {0x20,0x00,0xc0,0x00},
  {0x40,0x00,0xc0,0x00},
  {0x60,0x00,0xc0,0x00},
  {0x80,0x00,0xc0,0x00},
  {0xa0,0x00,0xc0,0x00},
  {0xc0,0x00,0xc0,0x00},
  {0xe0,0x00,0xc0,0x00},
  {0x00,0x20,0xc0,0x00},
  {0x20,0x20,0xc0,0x00},
  {0x40,0x20,0xc0,0x00},
  {0x60,0x20,0xc0,0x00},
  {0x80,0x20,0xc0,0x00},
  {0xa0,0x20,0xc0,0x00},
  {0xc0,0x20,0xc0,0x00},
  {0xe0,0x20,0xc0,0x00},
  {0x00,0x40,0xc0,0x00},
  {0x20,0x40,0xc0,0x00},
  {0x40,0x40,0xc0,0x00},
  {0x60,0x40,0xc0,0x00},
  {0x80,0x40,0xc0,0x00},
  {0xa0,0x40,0xc0,0x00},
  {0xc0,0x40,0xc0,0x00},
  {0xe0,0x40,0xc0,0x00},
  {0x00,0x60,0xc0,0x00},
  {0x20,0x60,0xc0,0x00},
  {0x40,0x60,0xc0,0x00},
  {0x60,0x60,0xc0,0x00},
  {0x80,0x60,0xc0,0x00},
  {0xa0,0x60,0xc0,0x00},
  {0xc0,0x60,0xc0,0x00},
  {0xe0,0x60,0xc0,0x00},
  {0x00,0x80,0xc0,0x00},
  {0x20,0x80,0xc0,0x00},
  {0x40,0x80,0xc0,0x00},
  {0x60,0x80,0xc0,0x00},
  {0x80,0x80,0xc0,0x00},
  {0xa0,0x80,0xc0,0x00},
  {0xc0,0x80,0xc0,0x00},
  {0xe0,0x80,0xc0,0x00},
  {0x00,0xa0,0xc0,0x00},
  {0x20,0xa0,0xc0,0x00},
  {0x40,0xa0,0xc0,0x00},
  {0x60,0xa0,0xc0,0x00},
  {0x80,0xa0,0xc0,0x00},
  {0xa0,0xa0,0xc0,0x00},
  {0xc0,0xa0,0xc0,0x00},
  {0xe0,0xa0,0xc0,0x00},
  {0x00,0xc0,0xc0,0x00},
  {0x20,0xc0,0xc0,0x00},
  {0x40,0xc0,0xc0,0x00},
  {0x60,0xc0,0xc0,0x00},
  {0x80,0xc0,0xc0,0x00},
  {0xa0,0xc0,0xc0,0x00},
  {0xff,0xfb,0xf0,0x00},
  {0xa0,0xa0,0xa4,0x00},
  {0x80,0x80,0x80,0x00},
  {0xff,0x00,0x00,0x00},
  {0x00,0xff,0x00,0x00},
  {0xff,0xff,0x00,0x00},
  {0x00,0x00,0xff,0x00},
  {0xff,0x00,0xff,0x00},
  {0x00,0xff,0xff,0x00},
  {0xff,0xff,0xff,0x00}};

//
static dev_panel_info_t g_panel_info={0};
static const unsigned char at91sam9261_lcd_tblum[16] =
{255,250,245,240,235,230,225,215,205,195,185,175,165,135,75,5};

static const unsigned char at91sam9261_lcd_lum = 13;

static PALETTEENTRY* g_at91sam79261_current_palette;
/*============================================
| Implementation
==============================================*/
/*--------------------------------------------
| Name:        at91sam9261_lcd_set_lut
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
void at91sam9261_lcd_set_lut(const PALETTEENTRY *palette)
{
   unsigned int i;

   g_at91sam79261_current_palette = (PALETTEENTRY*)palette;

   for(i =0; i <256; i++)
      g_panel_info.controller.lcdc->LCDC_LUT_ENTRY[i] = ((palette[i].red) >> 3 |
                                                         ((palette[i].green & 0xf8) << 2) |
                                                         ((palette[i].blue & 0xf8) << 7));
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_switch_active_frame_buffer
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
unsigned long at91sam9261_lcd_switch_active_frame_buffer(void){
   at91sam9261_shadow_frame_buffer = at91sam9261_current_frame_buffer;
   at91sam9261_current_frame_buffer=!at91sam9261_current_frame_buffer;

   g_panel_info.controller.lcdc->LCDC_BA1=
      (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_current_frame_buffer];
   return (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_shadow_frame_buffer];
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_get_current_frame_buffer
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
unsigned long at91sam9261_lcd_get_current_frame_buffer(void)
{
   return (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_current_frame_buffer];
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_get_shadow_frame_buffer
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
unsigned long at91sam9261_lcd_get_shadow_frame_buffer(void)
{
   return (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_shadow_frame_buffer];
}



/*--------------------------------------------
| Name:        at91sam9261_lcd_gpio
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
static void at91sam9261_lcd_gpio (void)
{
   AT91_SYS->PIOB_PDR =
      (AT91C_PB0_LCDVSYNC | AT91C_PB1_LCDHSYNC | AT91C_PB2_LCDDOTCK | AT91C_PB3_LCDDEN  |
       AT91C_PB4_LCDCC    | AT91C_PB7_LCDD2    | AT91C_PB8_LCDD3   |
       AT91C_PB9_LCDD4    | AT91C_PB10_LCDD5   | AT91C_PB11_LCDD6  |
       AT91C_PB12_LCDD7   | AT91C_PB15_LCDD10  | AT91C_PB16_LCDD11 |
       AT91C_PB17_LCDD12  | AT91C_PB18_LCDD13  | AT91C_PB19_LCDD14 |
       AT91C_PB20_LCDD15  | AT91C_PB23_LCDD18  | AT91C_PB24_LCDD19 |
       AT91C_PB25_LCDD20  | AT91C_PB26_LCDD21  | AT91C_PB27_LCDD22 |
       AT91C_PB28_LCDD23);

   AT91_SYS->PIOB_BSR = (AT91C_PB23_LCDD18 | AT91C_PB24_LCDD19 | AT91C_PB25_LCDD20 |
                         AT91C_PB26_LCDD21 | AT91C_PB27_LCDD22 | AT91C_PB28_LCDD23);

   /* Enable signaux cde cran LCD */
   AT91_SYS->PIOB_PER = AT91C_PIO_PB5 | AT91C_PIO_PB6;        // Enable PIO fonction
   AT91_SYS->PIOB_OER = AT91C_PIO_PB5 | AT91C_PIO_PB6;        // in Output fonction
   AT91_SYS->PIOB_CODR = AT91C_PIO_PB5 | AT91C_PIO_PB6;       // EN-DATALCD=0 , ALIM-LCD= 0
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_calc_frame_buffer_size
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
unsigned long at91sam9261_lcd_calc_frame_buffer_size(void)
{
   unsigned long size;
   int line_length =
      ((unsigned int)g_panel_info.vl_col * (unsigned int)NBITS(g_panel_info.vl_bpix)) / 8;

   size = line_length * (unsigned int)g_panel_info.vl_row;
   size += PAGE_SIZE;

   return size;
}


/*--------------------------------------------
| Name:        at91sam9261_lcd_ctrl_init
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
int at91sam9261_lcd_ctrl_init (dev_panel_info_t* p_panel_info)
{
   unsigned long value;
   unsigned int i,j;
   //
   if(!p_panel_info)
      return -1;
   //
   memcpy(&g_panel_info,p_panel_info,sizeof(dev_panel_info_t));
   //
   at91sam9261_lcd_gpio();                    // Configure les I/O pour le controleur LCD
   //
   AT91_SYS->PMC_PCER = 1 << AT91C_ID_LCDC;    // Enable LCD peripheral clock
   AT91_SYS->PMC_SCER = AT91C_PMC_HCK1;        // Enable HCLOCK in PMC (AHB LCDC Clock Output)
   /* DMA disable */
   g_panel_info.controller.lcdc->LCDC_DMACON = 0;
   /* LCD power off */
   //g_panel_info.controller.lcdc->LCDC_PWRCON = 0;  // Guard time = 0 => immediat


   lcd_console_address = p_at91sam9261_frame_buffer[at91sam9261_current_frame_buffer]; //Adresse du Frame buffer ds la SDRAM
   lcd_line_length = (g_panel_info.vl_col * NBITS (g_panel_info.vl_bpix)) / 8;   // Nb d'octet de data par ligne d'cran

   /* Init controller field in panel_info */
   g_panel_info.controller.lcdc = (AT91PS_LCDC) AT91C_BASE_LCDC;     // Initialise la structure pour pointer
   g_panel_info.controller.frame_buffer =
      (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_current_frame_buffer];                                      // sur les registres du controleur LCD

   /* Turn off the LCD controller and the DMA controller */
   g_panel_info.controller.lcdc->LCDC_PWRCON = 0x0C;  // LCD_PWR = 0 et GUARD_TIME = 6
   g_panel_info.controller.lcdc->LCDC_DMACON = 0;     // DMA Enable = 0

   // Reset LCDC DMA
   g_panel_info.controller.lcdc->LCDC_DMACON = AT91C_LCDC_DMARST;

   /* set frame size and burst length */
   value =
      ((unsigned int)g_panel_info.vl_row * (unsigned int)g_panel_info.vl_col *
       (unsigned int)NBITS(g_panel_info.vl_bpix)) / 32;
   value |= (unsigned long)((128 - 1) << 24);       // Burst Lengh = 128 word (32 bit)
   g_panel_info.controller.lcdc->LCDC_FRMCFG = value;

   /* Set pixel clock */
   value = AT91C_MASTER_CLOCK_FOR_I2S / g_panel_info.vl_pixclock;
   if (AT91C_MASTER_CLOCK_FOR_I2S % g_panel_info.vl_pixclock)
      value++;
   value = (value / 2) - 1;
   /* if (!value)
   g_panel_info.controller.lcdc->LCDC_LCDCON1 = AT91C_LCDC_BYPASS;
   else*/
   g_panel_info.controller.lcdc->LCDC_LCDCON1 = value << 12;     // =>AT91C_LCDC_BYPASS = 0

   /* Initialize control register 2 */
   if (g_panel_info.vl_tft)
      // Memory format= Little Endian, Display=TFT, LCDDOTCK always active
      value = ( AT91C_LCDC_MEMOR_LITTLEIND | AT91C_LCDC_DISTYPE_TFT | AT91C_LCDC_CLKMOD);
   else
      value = ( AT91C_LCDC_MEMOR_LITTLEIND | AT91C_LCDC_CLKMOD);

   if (!((unsigned int)g_panel_info.vl_hsp & FB_SYNC_HOR_HIGH_ACT))
      value |= 1 << 10;                // INVLINE=1  => HSYNC active low
   if (!((unsigned int)g_panel_info.vl_vsp & FB_SYNC_VERT_HIGH_ACT))
      value |= 1 << 9;                    // INVFRAME=1 => VSYNC active low

   value |= (unsigned int)(g_panel_info.vl_bpix << 5);  // Pixel size

   g_panel_info.controller.lcdc->LCDC_LCDCON2 = value;

   //at91sam9261_lcd_set_lut(PalettePC2);              // Initialise lookup table color
   //at91sam9261_lcd_set_lut(monochromePalette);              // Initialise lookup table color
   at91sam9261_lcd_set_lut(rgbPalette);


   /* Vertical timing */
   value = (unsigned int)((g_panel_info.vl_vpw - 1) << 16);
   value |= (unsigned int)(g_panel_info.vl_bfw << 8);
   value |= (unsigned int)g_panel_info.vl_efw;
   value |= 8 << 24;                          // VHDLY
   g_panel_info.controller.lcdc->LCDC_TIM1 = value;

   /* Horizontal timing */
   value = (unsigned int)((g_panel_info.vl_elw - 1) << 21);
   value |= (unsigned int)((g_panel_info.vl_hpw - 1) << 8);
   value |= (unsigned int)(g_panel_info.vl_blw - 1);

   g_panel_info.controller.lcdc->LCDC_TIM2 = value;

   value = (unsigned int)((g_panel_info.vl_col - 1) << 21);
   value |= (unsigned int)(g_panel_info.vl_row - 1);

   g_panel_info.controller.lcdc->LCDC_LCDFRCFG = value;

   /* FIFO Threshold: Use formula from data sheet */
   value = SIDSAFB_FIFO_SIZE - (2 * SIDSAFB_DMA_BURST_LEN + 3);         //value = 512;
   g_panel_info.controller.lcdc->LCDC_FIFO = value;

   /* Toggle LCD_MODE every frame */
   value = 0;
   g_panel_info.controller.lcdc->LCDC_MVAL = value;

   for (i=0; i<100000; i++)
   {
      j = (j >> 1)*5;
      j--;
      j = (j >> 1)*5;                        // Tempo
   }
   AT91_SYS->PIOB_SODR = AT91C_PIO_PB5;        // EN-DATALCD=1

   // Set contrast
   value = AT91C_LCDC_PS_DIVIDEDBYEIGHT | AT91C_LCDC_POL_NEGATIVEPULSE |
           AT91C_LCDC_ENA_PWMGEMENABLED;
   g_panel_info.controller.lcdc->LCDC_CTRSTCON = value;
   g_panel_info.controller.lcdc->LCDC_CTRSTVAL = 0xFF;     // Extinction

   /* Disable all interrupts */
   g_panel_info.controller.lcdc->LCDC_IDR = ~0UL;

   g_panel_info.controller.lcdc->LCDC_BA1 =
      (unsigned long)p_at91sam9261_frame_buffer[at91sam9261_current_frame_buffer];
   g_panel_info.controller.lcdc->LCDC_FRMCFG =
      (SIDSAFB_DMA_BURST_LEN <<
       24) +
      ((unsigned int)g_panel_info.vl_col * (unsigned int)g_panel_info.vl_row *
       (unsigned int)NBITS(g_panel_info.vl_bpix)) / 32;
   /* DMA enable */
   g_panel_info.controller.lcdc->LCDC_DMACON = AT91C_LCDC_DMAEN;
   /* LCD power on */
   g_panel_info.controller.lcdc->LCDC_PWRCON = AT91C_LCDC_PWR | (6 << 1); // Guard time = 6 => delay 100 ms

   g_panel_info.controller.lcdc->LCDC_CTRSTVAL = at91sam9261_lcd_tblum[at91sam9261_lcd_lum];

   return 0;
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_get_palette
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
PALETTEENTRY * at91sam9261_lcd_get_palette(void) {
   return (PALETTEENTRY *)g_at91sam79261_current_palette;
}

/*--------------------------------------------
| Name:        at91sam9261_lcd_get_panel_info
| Description:
| Parameters:  none
| Return Type: none
| Comments:
| See:
----------------------------------------------*/
dev_panel_info_t* at91sam9261_lcd_get_panel_info(void){
   return &g_panel_info;
}

/*============================================
| End of Source  : at91sam9261_lcd.c
==============================================*/
